#ifndef LOGIC_H
#define LOGIC_H

/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool. 

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.
  
*/

#include <list>
#include <map>

#include "enumerations.h"
#include "eventmanager.h"

#include "objects.h"
#include "physics.h"

/*Implements the Game Logic. Responsible for keeping record of all objects present
in the world, managing the physics systems, updating the objects state and making
collisions happen and managing requests from the UI (e.g starting a new game)
*/

class CLogic
{
    class CLogicListener : public CBaseEventListener
    {
    public:
        CLogicListener(string name, CLogic *parent);
        virtual ~CLogicListener();

        virtual bool processEvent(EventPtr event);

    private:

        CLogic *mParent;

    };

    EventListenerPtr mEventListener;

    friend class CLogic::CLogicListener;

public:

    CLogic(string listenerName = "LogicListener");
    ~CLogic();

    //initialise the two physics engines.
    void InitPhysicsEngine(float simTimeStep, float gravityValue, float frictionCoeff, float airDensity);

    void ChangeEnvironmentOptions(float simTimeStep, float gravityValue, float frictionCoeff, float airDensity = 0);

    /*Updates the state of all the objects in the world.
    checks collisions, prevents objects from falling below the ground level and from
    intersecting each other, calls the physics system and moves the objects.
    This function is called once per main loop.
    */
    void UpdateState();

    void AttachObject(ObjectPtr object);

    void RemoveAllObjects();

    int GetBallCount();
    int GetStaticObjCount();

private:

    /*tests all pair of objects in the world for collisions. If one is found,
    calls the physics to update their velocities.*/
    void TestCollisions();

    void ResetBallConfiguration();

    /*Physics Engines. An instance of two different engines is created so that it
    will be possible to change the behaviour at runtime.*/
    PhysicsSysPtr mPhysics;

    //store all actors by type
    typedef  map<eObjectID, ObjectPtr> ObjectMap;

    ObjectMap mMovables;
    ObjectMap mStatic;

    struct SGameStatus {

        unsigned int activeBalls; //number of pool balls remaining on the table
        unsigned int cueBallPocketed; //number of times the cue ball has fallen in a pocket

        int score; //a point is scored for each ball pocketed, one is subtracted each time the cue ball falls in a pocket

        bool gameRunning; //the game is still running (balls are left on the table and ball 8 is still on the table)
        bool gameWon; //true if no balls remain and ball 8 has been pocketed last. false if ball 8 has been pocketed
        //before all the other balls.

    }mGameStatus;
};

#endif // LOGIC_H
